package com.lei.zjs.test;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CGLibProxy {

	public Cat getObjectProxy(Class clazz){
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(clazz);
		Callback callback = new MethodInterceptor() {
			
			@Override
			public Object intercept(Object proxy, Method method, Object[] args,MethodProxy methodProxy) throws Throwable {
				System.out.println("钓鱼");
				Object result = methodProxy.invokeSuper(proxy, args);
				return result;
			}
		};
		enhancer.setCallback(callback );
		
		Object create = enhancer.create();
		return (Cat) create;
	}
	
	public static void main(String[] args) throws InstantiationException, IllegalAccessException {
		CGLibProxy cgLibProxy = new CGLibProxy();
		Cat cat = cgLibProxy.getObjectProxy(Cat.class);
		cat.eat();
	}
}
